<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/10/19
 * Time: 11:12
 */

namespace app\api\controller;


use language\Languagemsg;
use think\Db;

class Shopgoods extends Base
{
    private $user_id; // 用户ID
    private $page; // 页码
    private $create_time;
    
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        $this->msg_language = new Languagemsg();
        $this->language = input('language') ? input('language') : 0;
        $this->user_id = input('user_id');
        $this->page = input('page') ? input('page') : 1;
        $this->create_time = date('Y-m-d H:i:s');
        $time = input('time');
        $value = input('value');
        $reqKey = input('reqKey');
        $auth = $this->checkAll($this->user_id, $time, $value, $reqKey);
        if($auth == 112){
            echo json_encode(['status'=>301, 'msg'=>$this->msg_language->otherLogin[$this->language]]);
            exit;
        }elseif($auth == 2111){
            echo json_encode(['status' => 303, 'msg' => $this->msg_language->frost[$this->language]]);
            exit;
        }
    }
    
    /**
     * 商品列表
     * @return \think\response\Json
     * goods_status      状态  1 上架中   2 已下架
     * page              页码
     */
    public function goodsList(){
        $goods_status = input('goods_status');  // 1 上架中   2 已下架
        $list = Db::table('up_goods')
            ->where('goods_user_id', $this->user_id)
            ->where('goods_status', $goods_status)
            ->field('goods_id, goods_name, goods_thumb, goods_sail_num, goods_type, goods_activity_date, 
            goods_activity_start_time, goods_activity_end_time, goods_activity_start_datetime, 
            goods_activity_end_datetime')
            ->order('goods_time desc')
            ->page($this->page, 15)
            ->select();
        if($list){
            foreach($list as $k => $v){
                // 价格、库存
                $spec = Db::table('up_goods_spec')
                    ->where('spec_goods_id', $v['goods_id'])
                    ->field('spec_price, spec_stock')
                    ->order('spec_price asc')
                    ->find();
                $list[$k]['goods_stock'] = $spec['spec_stock'];
                $list[$k]['goods_price'] = $spec['spec_price'];
                // 上架中商品拍卖、秒杀商品状态
                if($goods_status == 1){
                    if($v['goods_type'] == 1){
                        $list[$k]['goods_activity_status'] = 0;
                    }else{
                        $time = date('Y-m-d H:i');
                        if(strtotime($time) < strtotime($v['goods_activity_start_datetime'])){
                            $list[$k]['goods_activity_status'] = 1; // 未开始
                        }elseif(strtotime($time) > strtotime($v['goods_activity_start_datetime']) && strtotime($time) < strtotime($v['goods_activity_end_datetime'])){
                            $list[$k]['goods_activity_status'] = 2; // 进行中
                        }else{
                            $list[$k]['goods_activity_status'] = 3; // 已结束
                        }
                    }
                }
            }
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 店铺主营商品类型
     * @return \think\response\Json
     */
    public function shopGoodsType(){
        $types = Db::table('up_shop')
            ->where('shop_user_id', $this->user_id)
            ->field('shop_goods_type')
            ->find();
        if($types){
            $typeArr = Db::table('up_goods_type')
                ->where('gt_id', 'IN', $types['shop_goods_type'])
                ->where('gt_del', 1)
                ->field('gt_id, gt_name')
                ->select();
            return json(['status' => 200, 'msg' => $typeArr]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 添加正常商品
     * @return \think\response\Json
     * goods     商品
     *      goods_name              商品名称
     *      goods_thumb             商品图片
     *      goods_cate              商品种类
     *      goods_detail            详情
     *      goods_detail_images     详情图片
     *      goods_video             视频
     *      goods_spec_name         规格名称
     *      goods_spec              正常商品规格（数组）
     *          spec_price          正常商品价格
     *          spec_old_price      正常商品原价（可不填）
     *          spec_can            正常商品规格参数
     *          spec_stock          正常商品库存
     */
    public function addCommonGoods(){
        $goods = input('goods');
        $goods = json_decode($goods, true);
        $goods_name = $goods['goods_name'];
        $goods_thumb = $goods['goods_thumb'];
        $goods_cate = $goods['goods_cate'];
        $goods_spec_name = $goods['goods_spec_name'];
        $goods_detail = $goods['goods_detail'];
        $goods_detail_images = $goods['goods_detail_images'];
        $goods_video = $goods['goods_video'];
        $goods_spec = $goods['goods_spec']; // 商品规格（数组）
        if(!is_array($goods_spec)){
//            return json(['status' => 0, 'msg' => '数据格式错误']);
            return json(['status' => 0, 'msg' => $this->msg_language->formatErr[$this->language]]);
        }
        
        $shop_id = Db::table('up_shop')->where('shop_user_id', $this->user_id)->value('shop_id');
        $insert = [
            'goods_name' => $goods_name,
            'goods_thumb' => $goods_thumb,
            'goods_cate' => $goods_cate,
            'goods_detail' => $goods_detail,
            'goods_detail_images' => $goods_detail_images,
            'goods_video' => $goods_video,
            'goods_type' => 1,
            'goods_shop_id' => $shop_id,
            'goods_user_id' => $this->user_id,
            'goods_time' => $this->create_time,
            'goods_spec_name' => $goods_spec_name,
            'goods_spec_min_price' => $this->getMinPrice($goods_spec)
        ];
        Db::startTrans();
        try{
            // 插入商品表
            $goods_id = Db::table('up_goods')->insertGetId($insert);
            if($goods_id){
                // 规格表
                if(!empty($goods_spec)){
                    $insertSpec = [];
                    foreach($goods_spec as $k => $v){
                        $insertSpec[$k] = [
                            'spec_goods_id' => $goods_id,
                            'spec_name' => $goods_spec_name,
                            'spec_price' => $v['spec_price'],
                            'spec_old_price' => $v['spec_old_price'],
                            'spec_can' => $v['spec_can'],
                            'spec_stock' => $v['spec_stock']
                        ];
                    }
                    Db::table('up_goods_spec')->insertAll($insertSpec);
                }
            }
            Db::commit();
//            return json(['status' => 200, 'msg' => '添加成功']);
            return json(['status' => 200, 'msg' => $this->msg_language->addSuccess[$this->language]]);
        }catch(\Exception $e){
            Db::rollback();
            return json(['status' => 0, 'msg' => $e->getMessage()]);
        }
    }
    
    /**
     * 添加拍卖商品
     * @return \think\response\Json
     * goods     商品
     *      goods_name              商品名称
     *      goods_thumb             商品图片
     *      goods_cate              商品种类
     *      goods_detail            详情
     *      goods_detail_images     详情图片
     *      goods_video             视频
     *      goods_activity_date     参加拍卖日期
     *      goods_activity_time     参加拍卖起始时间
     *      goods_spec_name         规格名称
     *      goods_bz_price          拍卖保证金
     *      goods_fudu              拍卖加价幅度
     *      goods_spec              正常商品规格（数组）
     *          spec_price          正常商品价格
     *          spec_can            正常商品规格参数
     *          spec_stock          正常商品库存
     */
    public function addPaiMaiGoods(){
        $goods = input('goods');
        $goods = json_decode($goods, true);
        $goods_name = $goods['goods_name'];
        $goods_thumb = $goods['goods_thumb'];
        $goods_cate = $goods['goods_cate'];
        $goods_detail = $goods['goods_detail'];
        $goods_detail_images = $goods['goods_detail_images'];
        $goods_video = $goods['goods_video'];
        $goods_activity_date = $goods['goods_activity_date']; // 参加拍卖日期
        $goods_activity_time = $goods['goods_activity_time']; // 参加拍卖起止时间
        $goods_spec_name = $goods['goods_spec_name'];
        $goods_bz_price = $goods['goods_bz_price'];
        $goods_fudu = $goods['goods_fudu'];
        $goods_spec = $goods['goods_spec']; // 商品规格
        if(!is_array($goods_spec)){
//            return json(['status' => 0, 'msg' => '数据格式错误']);
            return json(['status' => 0, 'msg' => $this->msg_language->formatErr[$this->language]]);
        }
        $shop_id = Db::table('up_shop')->where('shop_user_id', $this->user_id)->value('shop_id');
        $timeArr = explode('-', $goods_activity_time);
        $goods_activity_start_time = $timeArr[0];
        $goods_activity_end_time = $timeArr[1];
    
        $insert = [
            'goods_name' => $goods_name,
            'goods_thumb' => $goods_thumb,
            'goods_detail' => $goods_detail,
            'goods_detail_images' => $goods_detail_images,
            'goods_video' => $goods_video,
            'goods_cate' => $goods_cate,
            'goods_type' => 2,
            'goods_shop_id' => $shop_id,
            'goods_user_id' => $this->user_id,
            'goods_time' => $this->create_time,
            'goods_spec_name' => $goods_spec_name,
            'goods_spec_min_price' => $this->getMinPrice($goods_spec),
            'goods_activity_date' => $goods_activity_date,
            'goods_activity_start_time' => $goods_activity_start_time,
            'goods_activity_end_time' => $goods_activity_end_time,
            'goods_bz_price' => $goods_bz_price,
            'goods_fudu' => $goods_fudu,
            'goods_now_price' => $this->getMinPrice($goods_spec),
            'goods_activity_start_datetime' => $goods_activity_date . ' ' . $goods_activity_start_time,
            'goods_activity_end_datetime' => $goods_activity_date . ' ' . $goods_activity_end_time
        ];
    
        Db::startTrans();
        try{
            // 插入商品表
            $goods_id = Db::table('up_goods')->insertGetId($insert);
            if($goods_id){
                // 规格表
                if(!empty($goods_spec)){
                    $insertSpec = [
                        'spec_goods_id' => $goods_id,
                        'spec_name' => $goods_spec_name,
                        'spec_price' => $goods_spec['spec_price'],
                        'spec_can' => $goods_spec['spec_can'],
                        'spec_stock' => $goods_spec['spec_stock']
                    ];
                    Db::table('up_goods_spec')->insert($insertSpec);
                }
            }
            Db::commit();
//            return json(['status' => 200, 'msg' => '操作成功']);
            return json(['status' => 200, 'msg' => $this->msg_language->doSuccess[$this->language]]);
        }catch(\Exception $e){
            Db::rollback();
            return json(['status' => 0, 'msg' => $e->getMessage()]);
        }
    }
    
    /**
     * 添加秒杀商品
     * @return \think\response\Json
     * goods      商品
     *      goods_id                商品ID
     *      goods_activity_date     参加秒杀日期
     *      goods_activity_time     参加秒杀起止时间
     *      goods_spec              商品规格（数组）
     *          spec_price          规格价格
     *          spec_can            参数名称
     *          spec_stock          规格库存
     */
    public function addMiaoshaGoods(){
        $goods = input('goods');
        $goods = json_decode($goods, true);
        $goods_id = $goods['goods_id'];
        $goods_activity_date = $goods['goods_activity_date']; // 参加秒杀日期
        $goods_activity_time = $goods['goods_activity_time']; // 参加秒杀起止时间
        $goods_spec = $goods['goods_spec']; // 正常商品规格
        if(!is_array($goods_spec)){
//            return json(['status' => 0, 'msg' => '数据格式错误']);
            return json(['status' => 0, 'msg' => $this->msg_language->formatErr[$this->language]]);
        }
        // 商品信息
        $goodsDetail = Db::table('up_goods')
            ->where('goods_id', $goods_id)
            ->field('goods_name, goods_thumb, goods_detail, goods_detail_images, goods_video, goods_cate, 
        goods_shop_id, goods_user_id, goods_spec_name')
            ->find();

        $timeArr = explode('-', $goods_activity_time);
        $goods_activity_start_time = $timeArr[0];
        $goods_activity_end_time = $timeArr[1];
        $insert = [
            'goods_name' => $goodsDetail['goods_name'],
            'goods_thumb' => $goodsDetail['goods_thumb'],
            'goods_detail' => $goodsDetail['goods_detail'],
            'goods_detail_images' => $goodsDetail['goods_detail_images'],
            'goods_video' => $goodsDetail['goods_video'],
            'goods_cate' => $goodsDetail['goods_cate'],
            'goods_type' => 3,
            'goods_shop_id' => $goodsDetail['goods_shop_id'],
            'goods_user_id' => $goodsDetail['goods_user_id'],
            'goods_time' => $this->create_time,
            'goods_spec_min_price' => $this->getMinPrice($goods_spec),
            'goods_activity_date' => $goods_activity_date,
            'goods_activity_start_time' => $goods_activity_start_time,
            'goods_activity_end_time' => $goods_activity_end_time,
            'goods_spec_name' => $goodsDetail['goods_spec_name'],
            'goods_activity_start_datetime' => $goods_activity_date . ' ' . $goods_activity_start_time,
            'goods_activity_end_datetime' => $goods_activity_date . ' ' . $goods_activity_end_time
        ];
        
        Db::startTrans();
        try{
            $new_goods_id = Db::table('up_goods')->insertGetId($insert);
            if($new_goods_id){
                // 规格表
                if(!empty($goods_spec)){
                    $insertSpec = [];
                    foreach($goods_spec as $k => $v){
                        $insertSpec[$k] = [
                            'spec_goods_id' => $new_goods_id,
                            'spec_name' => $goodsDetail['goods_spec_name'],
                            'spec_price' => $v['spec_price'],
                            'spec_can' => $v['spec_can'],
                            'spec_stock' => $v['spec_stock'],
                            'spec_old_price' => $v['spec_old_price']
                        ];
                    }
                    Db::table('up_goods_spec')->insertAll($insertSpec);
                }
            }
            Db::commit();
//            return json(['status' => 200, 'msg' => '添加成功']);
            return json(['status' => 200, 'msg' => $this->msg_language->addSuccess[$this->language]]);
        }catch(\Exception $e){
            Db::rollback();
            return json(['status' => 0, 'msg' => $e->getMessage()]);
        }
    }
    
    /**
     * 上架/下架/删除
     * @return \think\response\Json
     * goods_id     商品ID
     * goods_status   1 上架   2 下架   3 删除
     */
    public function upOrDown(){
        $goods_id = input('goods_id');
        $goods_status = input('goods_status');
        $goods = Db::table('up_goods')
            ->where('goods_id', $goods_id)
            ->field('goods_type, goods_activity_start_datetime, goods_activity_end_datetime')
            ->find();
        // 秒杀/拍卖商品，活动进行中不允许下架或删除
        if($goods['goods_type'] != 1 && $goods_status != 1){
            $time = date('Y-m-d H:i');
            if(strtotime($time) > strtotime($goods['goods_activity_start_datetime']) && strtotime($time) < strtotime($goods['goods_activity_end_datetime'])){
//                return json(['status' => 0, 'msg' => '活动已开始，不允许下架或删除']);
                return json(['status' => 0, 'msg' => $this->msg_language->refuseDown[$this->language]]);
            }
        }
        $res = Db::table('up_goods')
            ->where('goods_id', $goods_id)
            ->update(['goods_status' => $goods_status, 'goods_update_time' => $this->create_time]);
        if($res){
//            return json(['status' => 200, 'msg' => '操作成功']);
            return json(['status' => 200, 'msg' => $this->msg_language->doSuccess[$this->language]]);
        }else{
//            return json(['status' => 0, 'msg' => '系统异常，请稍后重试']);
            return json(['status' => 0, 'msg' => $this->msg_language->systemErr[$this->language]]);
        }
    }
    
    /**
     * 判断某个场次活动报名是否已满
     * @return \think\response\Json
     * activity_type      2 拍卖   3 秒杀
     * goods_activity_date    日期
     * goods_activity_time    起止时间
     */
    public function isFull(){
        $activity_type = input('activity_type'); // 2 拍卖   3 秒杀
        $goods_activity_date = input('goods_activity_date');
        $goods_activity_time = input('goods_activity_time');
        $timeArr = explode('-', $goods_activity_time);
        $goods_activity_start_time = $timeArr[0];
        $goods_activity_end_time = $timeArr[1];
        
        // 限制可以参加的商品数量
        $limit_num = Db::table('up_activity_times')
            ->where('at_type', $activity_type)
            ->where('at_start_time', $goods_activity_start_time)
            ->where('at_end_time', $goods_activity_end_time)
            ->value('at_num');
        // 已参加商品数
        $already_num = Db::table('up_goods')
            ->where('goods_type', $activity_type)
            ->where('goods_activity_date', $goods_activity_date)
            ->where('goods_activity_start_time', $goods_activity_start_time)
            ->where('goods_activity_end_time', $goods_activity_end_time)
            ->count();
        if($already_num >= $limit_num){
//            return json(['status' => 0, 'msg' => '该场次活动名额已满']);
            return json(['status' => 0, 'msg' => $this->msg_language->numFull[$this->language]]);
        }else{
            return json(['status' => 200, 'msg' => true]);
        }
    }
    
    /**
     * 商品详情展示
     * @return \think\response\Json
     * goods_id    商品ID
     */
    public function showGoods(){
        $goods_id = input('goods_id');
        $detail = Db::table('up_goods')
            ->where('goods_id', $goods_id)
            ->join('up_goods_type', 'gt_id = goods_cate', 'LEFT')
            ->field('goods_name, goods_thumb, gt_name, goods_cate, goods_detail, goods_detail_images, 
            goods_video, goods_activity_date, goods_activity_start_time, goods_activity_end_time, 
            goods_bz_price, goods_fudu, goods_spec_name, goods_type, goods_id')
            ->find();
        // 图片
        if(!empty($detail['goods_detail_images'])){
            $image = explode(',', trim($detail['goods_detail_images'], ','));
            $detail['goods_detail_images'] = $image;
        }else{
            $detail['goods_detail_images'] = [];
        }
        // 规格
        $spec = Db::table('up_goods_spec')
            ->where('spec_goods_id', $goods_id)
            ->field('spec_id, spec_name, spec_price, spec_old_price, spec_can, spec_stock')
            ->select();
        $detail['spec'] = $spec;
        // 如果是拍卖商品或秒杀商品，拼接起止时间
        if($detail['goods_type'] == 2 || $detail['goods_type'] == 3){
            $detail['activity_time'] = $detail['goods_activity_start_time'] . '-' . $detail['goods_activity_end_time'];
        }
        return json(['status' => 200, 'msg' => $detail]);
    }
    
    /**
     * 编辑正常商品
     * @return \think\response\Json
     * goods     商品
     *      goods_id                商品ID
     *      goods_name              商品名称
     *      goods_thumb             商品图片
     *      goods_cate              商品种类
     *      goods_detail            详情
     *      goods_detail_images     详情图片
     *      goods_video             视频
     *      goods_spec_name         规格名称
     *      goods_spec              商品规格（数组）
     *          spec_id             规格ID，新增的传-1
     *          spec_price          正常商品价格
     *          spec_old_price      正常商品原价（可不填）
     *          spec_can            正常商品规格参数
     *          spec_stock          正常商品库存
     */
    public function updateCommonGoods(){
        $goods = input('goods');
        $goods = json_decode($goods, true);
        $goods_id = $goods['goods_id'];
        $goods_name = $goods['goods_name'];
        $goods_thumb = $goods['goods_thumb'];
        $goods_cate = $goods['goods_cate'];
        $goods_detail = $goods['goods_detail'];
        $goods_detail_images = $goods['goods_detail_images'];
        $goods_video = $goods['goods_video'];
        $goods_spec_name = $goods['goods_spec_name'];
        $goods_spec = $goods['goods_spec']; // 商品规格
        if(!is_array($goods_spec)){
//            return json(['status' => 0, 'msg' => '数据格式错误']);
            return json(['status' => 0, 'msg' => $this->msg_language->formatErr[$this->language]]);
        }
    
        $update = [
            'goods_name' => $goods_name,
            'goods_thumb' => $goods_thumb,
            'goods_detail' => $goods_detail,
            'goods_detail_images' => $goods_detail_images,
            'goods_video' => $goods_video,
            'goods_cate' => $goods_cate,
            'goods_update_time' => $this->create_time,
            'goods_spec_name' => $goods_spec_name,
            'goods_spec_min_price' => $this->getMinPrice($goods_spec),
            'goods_status' => 1
        ];
        
        Db::startTrans();
        try{
            // 更新商品
            Db::table('up_goods')->where('goods_id', $goods_id)->update($update);
            // 更新规格参数
            foreach($goods_spec as $k => $v){
                $updateSpec = [
                    'spec_name' => $goods_spec_name,
                    'spec_price' => $v['spec_price'],
                    'spec_old_price' => $v['spec_old_price'],
                    'spec_can' => $v['spec_can'],
                    'spec_stock' => $v['spec_stock']
                ];
                if($v['spec_id'] == -1){ // 新增的规格
                    $updateSpec['spec_goods_id'] = $goods_id;
                    Db::table('up_goods_spec')->insert($updateSpec);
                }else{
                    $updateSpec['spec_updatetime'] = $this->create_time;
                    Db::table('up_goods_spec')->where('spec_id', $v['spec_id'])->update($updateSpec);
                }
            }
            Db::commit();
//            return json(['status' => 200, 'msg' => '操作成功']);
            return json(['status' => 200, 'msg' => $this->msg_language->doSuccess[$this->language]]);
        }catch(\Exception $e){
            Db::rollback();
            return json(['status' => 0, 'msg' => $e->getMessage()]);
        }
    }
    
    /**
     * 编辑拍卖商品
     * @return \think\response\Json
     * goods     商品
     *      goods_id                商品ID
     *      goods_name              商品名称
     *      goods_thumb             商品图片
     *      goods_cate              商品种类
     *      goods_detail            详情
     *      goods_detail_images     详情图片
     *      goods_video             视频
     *      goods_activity_date     参加拍卖日期
     *      goods_activity_time     参加拍卖起始时间
     *      goods_spec_name         规格名称
     *      goods_bz_price          拍卖保证金
     *      goods_fudu              拍卖加价幅度
     *      goods_spec              正常商品规格（数组）
     *          spec_id             规格ID，新增的传-1
     *          spec_price          正常商品价格
     *          spec_can            正常商品规格参数
     *          spec_stock          正常商品库存
     */
    public function updatePaiMaiGoods(){
        $goods = input('goods');
        $goods = json_decode($goods, true);
        $goods_id = $goods['goods_id'];
        $goods_name = $goods['goods_name'];
        $goods_thumb = $goods['goods_thumb'];
        $goods_cate = $goods['goods_cate'];
        $goods_detail = $goods['goods_detail'];
        $goods_detail_images = $goods['goods_detail_images'];
        $goods_video = $goods['goods_video'];
        $goods_activity_date = $goods['goods_activity_date']; // 参加拍卖日期
        $goods_activity_time = $goods['goods_activity_time']; // 参加拍卖起止时间
        $goods_spec_name = $goods['goods_spec_name'];
        $goods_bz_price = $goods['goods_bz_price'];
        $goods_fudu = $goods['goods_fudu'];
        $goods_spec = $goods['goods_spec']; // 商品规格
        if(!is_array($goods_spec)){
//            return json(['status' => 0, 'msg' => '数据格式错误']);
            return json(['status' => 0, 'msg' => $this->msg_language->formatErr[$this->language]]);
        }
        $status = Db::table('up_goods')
            ->where('goods_id', $goods_id)
            ->field('goods_sta')
            ->find();
        if($status['goods_sta'] == 2){
            return json(['status' => 0, 'msg' => $this->msg_language->alreadyBegin[$this->language]]);
        }
        if($status['goods_sta'] == 3){
            return json(['status' => 0, 'msg' => $this->msg_language->alreadyOver[$this->language]]);
        }
        
        // 活动开始和结束时间
        $timeArr = explode('-', $goods_activity_time);
        $goods_activity_start_time = $timeArr[0];
        $goods_activity_end_time = $timeArr[1];
        $update = [
            'goods_name' => $goods_name,
            'goods_thumb' => $goods_thumb,
            'goods_detail' => $goods_detail,
            'goods_detail_images' => $goods_detail_images,
            'goods_video' => $goods_video,
            'goods_cate' => $goods_cate,
            'goods_update_time' => $this->create_time,
            'goods_spec_name' => $goods_spec_name,
            'goods_spec_min_price' => $this->getMinPrice($goods_spec),
            'goods_activity_date' => $goods_activity_date,
            'goods_activity_start_time' => $goods_activity_start_time,
            'goods_activity_end_time' => $goods_activity_end_time,
            'goods_bz_price' => $goods_bz_price,
            'goods_fudu' => $goods_fudu,
            'goods_now_price' => $this->getMinPrice($goods_spec),
            'goods_activity_start_datetime' => $goods_activity_date . ' ' . $goods_activity_start_time,
            'goods_activity_end_datetime' => $goods_activity_date . ' ' . $goods_activity_end_time,
            'goods_status' => 1
        ];
        
        Db::startTrans();
        try{
            // 商品表
            Db::table('up_goods')->where('goods_id', $goods_id)->update($update);
            // 更新规格参数
            foreach($goods_spec as $k => $v){
                $updateSpec = [
                    'spec_name' => $goods_spec_name,
                    'spec_price' => $v['spec_price'],
                    'spec_can' => $v['spec_can'],
                    'spec_stock' => $v['spec_stock']
                ];
                if($v['spec_id'] == -1){ // 新增的规格
                    $updateSpec['spec_goods_id'] = $goods_id;
                    Db::table('up_goods_spec')->insert($updateSpec);
                }else{
                    $updateSpec['spec_updatetime'] = $this->create_time;
                    Db::table('up_goods_spec')->where('spec_id', $v['spec_id'])->update($updateSpec);
                }
            }
            
            Db::commit();
//            return json(['status' => 200, 'msg' => '操作成功']);
            return json(['status' => 200, 'msg' => $this->msg_language->doSuccess[$this->language]]);
        }catch(\Exception $e){
            Db::rollback();
            return json(['status' => 0, 'msg' => $e->getMessage()]);
        }
    }
    
    /**
     * 编辑秒杀商品
     * @return \think\response\Json
     * goods      商品
     *      goods_id                商品ID
     *      goods_activity_date     参加秒杀日期
     *      goods_activity_time     参加秒杀起止时间
     *      goods_spec              商品规格（数组）
     *          spec_price          规格价格
     *          spec_can            参数名称
     *          spec_stock          规格库存
     */
    public function updateMiaoShaGoods(){
        $goods = input('goods');
        $goods = json_decode($goods, true);
        $goods_id = $goods['goods_id'];
        $goods_activity_date = $goods['goods_activity_date']; // 参加秒杀日期
        $goods_activity_time = $goods['goods_activity_time']; // 参加秒杀起止时间
        $goods_spec = $goods['goods_spec']; // 商品规格
        if(!is_array($goods_spec)){
//            return json(['status' => 0, 'msg' => '数据格式错误']);
            return json(['status' => 0, 'msg' => $this->msg_language->formatErr[$this->language]]);
        }
        $status = Db::table('up_goods')
            ->where('goods_id', $goods_id)
            ->field('goods_sta')
            ->find();
        if($status['goods_sta'] == 2){
            return json(['status' => 0, 'msg' => $this->msg_language->alreadyBegin[$this->language]]);
        }
        if($status['goods_sta'] == 3){
            return json(['status' => 0, 'msg' => $this->msg_language->alreadyOver[$this->language]]);
        }
    
        // 活动开始和结束时间
        $timeArr = explode('-', $goods_activity_time);
        $goods_activity_start_time = $timeArr[0];
        $goods_activity_end_time = $timeArr[1];
        $update = [
            'goods_activity_date' => $goods_activity_date,
            'goods_activity_start_time' => $goods_activity_start_time,
            'goods_activity_end_time' => $goods_activity_end_time,
            'goods_update_time' => $this->create_time,
            'goods_spec_min_price' => $this->getMinPrice($goods_spec),
            'goods_activity_start_datetime' => $goods_activity_date . ' ' . $goods_activity_start_time,
            'goods_activity_end_datetime' => $goods_activity_date . ' ' . $goods_activity_end_time,
            'goods_status' => 1
        ];
        
        Db::startTrans();
        try{
            // 商品表
            Db::table('up_goods')->where('goods_id', $goods_id)->update($update);
            // 更新规格参数
            foreach($goods_spec as $k => $v){
                $updateSpec = [
                    'spec_price' => $v['spec_price'],
                    'spec_can' => $v['spec_can'],
                    'spec_stock' => $v['spec_stock'],
                    'spec_old_price' => $v['spec_old_price']
                ];
                if($v['spec_id'] == -1){ // 新增的规格
                    $updateSpec['spec_goods_id'] = $goods_id;
                    Db::table('up_goods_spec')->insert($updateSpec);
                }else{
                    $updateSpec['spec_updatetime'] = $this->create_time;
                    Db::table('up_goods_spec')->where('spec_id', $v['spec_id'])->update($updateSpec);
                }
            }
            
            Db::commit();
//            return json(['status' => 200, 'msg' => '操作成功']);
            return json(['status' => 200, 'msg' => $this->msg_language->doSuccess[$this->language]]);
        }catch(\Exception $e){
            Db::rollback();
            return json(['status' => 0, 'msg' => $e->getMessage()]);
        }
    }
    
    /**
     * 获取最低价格
     * @param $goods_spec   商品规格数组
     * @return int
     */
    private function getMinPrice($goods_spec){
        if(!empty($goods_spec)){
            if(count($goods_spec) == count($goods_spec, 1)){ // 一维数组
                $min_price = $goods_spec['spec_price'];
            }else{
                $priceArr = array_column($goods_spec, 'spec_price');
                sort($priceArr);
                $min_price = $priceArr[0];
            }
        }else{
            $min_price = 0;
        }
        return $min_price;
    }
}